package 集合框架.Map.购物车;

import java.util.List;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("欢迎访问淘宝网");
        while (true) {
            System.out.println("\t1.商品列表  \t2.我的购物车 \t 0.登出");
            System.out.print("请选择：");
            int choice = input.nextInt();
            switch (choice) {
                case 1: {
                    String reply = null;
                    do {
                        System.out.println("所有商品信息列表如下：");
                        System.out.println("-----------------------------------------");
                        System.out.println("序号\t\t编号\t\t\t\t\t商品\t\t\t\t\t单价\t\t\t库存");
                        System.out.println("-----------------------------------------");
                        List<Goods> goodsList = CartManager.getGoodsList();
                        int k = 0;
                        for (Goods goods : goodsList) {
                            System.out.println((++k) + "\t\t" + goods.getId() + "\t\t" + goods.getTitle() + "\t\t" + goods.getPrice() + "\t\t" + goods.getStock());

                        }
                        System.out.println("-----------------------------------------");
                        System.out.println("请选择：");
                        int seqNo = input.nextInt();
                        int index = seqNo - 1;
                        Goods goods = CartManager.getGoodsByIndex(index);
                        CartManager.addToCart(goods);
                        System.out.println("成功添加到购物车！");
                        System.out.print("继续购买商品吗,是否确认(Y/N)?)");
                        reply = input.next();
                        if ("n".equalsIgnoreCase(reply)) {
                            break;
                        }
                    } while (true);

                    break;
                }
                case 2: {
                    List<Cartltem> cartItemList = CartManager.getCarItemList();
                    System.out.println("序号\t\t商品\t\t单价\t\t数量\t\t小计");
                    for (int i = 0; i < cartItemList.size(); i++) {
                        Cartltem cartItem = cartItemList.get(i);
                        System.out.println((i + 1) + "\t\t" + cartItem.getGoods().getTitle() + "\t\t" + cartItem.getGoods().getPrice() + "\t\t" + cartItem.getQuantity() + "\t\t" + cartItem.getSubTotal());
                    }
                    System.out.println("\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t总价：" + CartManager.calcTotalPrice() + "元");
                    do {
                        boolean flag = false;
                        System.out.println("1.移除商品");
                        System.out.println("2.清空购物车");
                        System.out.println("3.更改数量");
                        System.out.println("4.去结算");
                        System.out.print("输入0，返回上一级菜单，请选择：");
                        int choose = input.nextInt();
                        switch (choose) {
                            case 1:
                                System.out.println("请输入想要移除的商品编号:");
                                int goodsIdKey = input.nextInt();
                                CartManager.removeFromCart(goodsIdKey);
                                break;
                            case 2:
                                CartManager.clearCart();
                                break;
                            case 3:
                                System.out.println("请输入商品编号：");
                                goodsIdKey = input.nextInt();
                                System.out.println("请输入商品数量：");
                                int qty = input.nextInt();
                                CartManager.changeQty(goodsIdKey, qty);
                                break;
                            case 4:
                                CartManager.doPay();
                                break;
                            case 0:
                                flag = true;
                                break;
                        }
                        if (flag) {
                            break;
                        }
                    } while (true);

                    break;
                }
                case 3 -> {
                    System.out.println("感谢惠顾，欢迎下次再来哦！！");
                    System.exit(0);
                    break;
                }
            }
        }
    }
}
